/*
 * FileDetailsTable.java
 *
 * Created on 05 May 2007, 15:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.cardcopier.ui;

import java.awt.Color;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import org.cardcopier.file.ByteSizeFormatter;
import org.cardcopier.file.FileState;
import org.cardcopier.file.FileUtils;

/**
 *
 * @author Mark
 */
public class FileDetailsTable extends JTable {
    
    private static class FileExistsCellRenderer extends DefaultTableCellRenderer {
    
        private static final String STATUS_FILE_EXISTS = "File Exists!";
        private static final String STATUS_FILE_NEW = "New";
        
        private static final Color FILE_EXISTS_COLOR = new Color(250, 80 , 80);

        private final DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
        private final ByteSizeFormatter byteSizeFormatter 
                = new ByteSizeFormatter(ByteSizeFormatter.Scale.KB, 0);
        
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            FileDetailsTableModel model = (FileDetailsTableModel) table.getModel();
            if (model.fileExistsInDestination(row)) {
                setBackground(FILE_EXISTS_COLOR);
            } else {
                setBackground(table.getBackground());
            }
            setHorizontalAlignment(SwingConstants.LEFT);
            if (value instanceof Date) {
                // Make our modification date pretty
                Date date = (Date)value;
                value = dateFormat.format(date);
                setHorizontalAlignment(SwingConstants.RIGHT);
            } else if (value instanceof Long) {
                // Show our filesize in Kilobytes
                Long fileSizeInBytes = (Long)value;
                value = byteSizeFormatter.format(fileSizeInBytes);
                setHorizontalAlignment(SwingConstants.RIGHT);
            } else if (value instanceof Boolean) {
                // Use some descriptive text for the file exists column
                Boolean b = (Boolean)value;
                value = b == Boolean.TRUE ? STATUS_FILE_EXISTS : STATUS_FILE_NEW; 
            }
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    }

   
    private final TableCellRenderer tableCellRenderer = new FileExistsCellRenderer();
    
    /** Creates a new instance of FileDetailsTable */
    public FileDetailsTable() {
    }

    public void setInitialColumnWidths() {
        getColumn(FileDetailsTableModel.FILE_NAME_COLUMN).setPreferredWidth(250);
        getColumn(FileDetailsTableModel.MODIFIED_DATE_COLUMN).setPreferredWidth(120);
    }

    public void setFileStates(List<FileState> fileStates) {
        clearSelection();
        // Clear out the lead and anchor as well so that we do not get old values from the
        // old size of the fileStates.
        getSelectionModel().removeSelectionInterval(0, 0);
        getFileDetailsTableModel().setFileStates(fileStates);
        // If all of the files in the source are new then select them all to
        // be copied by default
        boolean allNew = true;
        for (int i = 0; i < fileStates.size() && allNew; i++) {
            FileState fileState = fileStates.get(i);
            allNew &= !fileState.fileExistsInDestination();
        }
        if (allNew) {
            selectAll();
        }
    }

    public FileDetailsTableModel getFileDetailsTableModel() {
        return (FileDetailsTableModel) getModel();
    }
    
    public TableCellRenderer getCellRenderer(int row, int column) {
        return tableCellRenderer;
    }

    public List<FileState> getSelectedFiles() {
        int[] selectRowIndices = getSelectedRows();
        FileDetailsTableModel model = getFileDetailsTableModel();
        List<FileState> selectedFiles = new ArrayList<FileState>();
        for(int i = 0; i < selectRowIndices.length; i++) {
            int selectedRow = selectRowIndices[i];
            selectedFiles.add(model.getFileStateAt(selectedRow));
        }
        return selectedFiles;
    }

}
